import requests
import http.client

conn = http.client.HTTPConnection('localhost', 80)
conn.request('GET', '/agileone/index.php')
content = conn.getresponse().read().decode()
# print(content)
# conn.close()

# 请求方法:get, post, delete, put
# 发起请求
r = requests.get('http://localhost/agileone/')
# 查看编码格式
# print(r.encoding)
r.encoding = 'utf-8'
# print(r.encoding)
# 获取响应.text是处理文本格式的响应. content是处理二进制响应内容, json是处理json格式的响应内容
# print(r.text)
# print(r.cookies)

# 发送post请求
# data = 'username=admin&password=admin&savelogin=true'
data = {'username': 'admin', 'password': 'admin', 'savelogin': 'true'}
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
r = requests.post('http://localhost/agileone/index.php/common/login', data=data, headers=headers)
# print(r.text)

# 重定向
# http.client和requests处理重定向的方式是有区别的.
# requests里, 默认情况下, 除了HEAD, 它会自动处理所有的重定向
# http.client不自动处理重定向
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
data = 'emp_no=admin&password=admin'
# 默认情况下allow_redirects = True. 如果不需要requests处理重定向, 请设置allow_redirects = False
r = requests.post('http://localhost/smeoa/index.php?m=login&a=check_login', data=data, headers=headers,
                  allow_redirects=False)
print(r.text)
# print(r.history)    #查看请求历史

headers = {'Content-Type': 'application/x-www-form-urlencoded'}
data = 'emp_no=admin&password=admin'
conn = http.client.HTTPConnection('localhost')
conn.request('POST', '/smeoa/index.php?m=login&a=check_login', body=data, headers=headers)
# print(conn.getresponse().read().decode())
